fix: prevent empty image object from being sent in freeform post creation#5874
Merged
fix: prevent empty image object from being sent in freeform post creation#5874
Conversation
…tion
When creating a freeform post without a thumbnail image, the image field
could be sent as an empty/invalid object ({}) to the API. This happens
because the form's file input produces a value that isn't a valid File
but is still truthy, so graphql-request serializes it as {} in the JSON
payload. The API then tries to process it as a file upload and crashes
with 'Unexpected error'.
Fix: strip the image field from the GraphQL variables unless it is a
valid File instance with non-zero size. Applied both at the call site
(create.tsx) and defensively in the shared createPostInMultipleSources
function.
Closes dailydotdev/daily#2095
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When creating a freeform post without a thumbnail image, the
imagefield is sent as an empty object{}in the GraphQL variables.graphql-requestv3 serializesFileobjects as{}in JSON sinceFilehas no enumerable own properties. The API receives this{}, enters the Cloudinary upload path (because{}is truthy), tries to callcreateReadStream()on it, and crashes with "Unexpected error".Reported in dailydotdev/daily#2095.
Fix
Strip the
imagefield from the mutation variables unless it is a validFileinstance with non-zero size:packages/webapp/pages/squads/create.tsx— destructureimageout of params and only spread it back when validpackages/shared/src/graphql/posts.ts— defensive sanitization increatePostInMultipleSources()before sending the requestTesting
Preview domain
https://fix-empty-image-object-freeform.preview.app.daily.dev